import java.util.ArrayList;
import java.util.Stack;
import java.util.Collections;
/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-06
 * Time:22:15
 */

/**
 * JZ6从尾到头打印链表
 */

public class printListFromTailToHead {
    public static void main(String[] args) {

    }
    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
//        Stack<ListNode> stack = new Stack<>();
//        while (listNode != null){
//            stack.push(listNode);
//            listNode = listNode.next;
//        }
//        ArrayList<Integer> list = new ArrayList<>();
//        while (stack.empty()){
//            list.add(stack.pop().val);
//        }
//        return list;
        ArrayList<Integer> list = new ArrayList<>();
        ListNode current = listNode;
        while (current != null) {
            list.add(current.val);
            current = current.next;
        }
        Collections.reverse(list);

        return list;
    }
}
